#include "ResourceManager.h"
#include <iostream>
#include <iomanip>

namespace common_component
{

    ResourceManager &ResourceManager::Instance()
    {
        static ResourceManager instance;
        return instance;
    }

    ResourceManager::~ResourceManager()
    {
        Shutdown();
    }

    std::shared_ptr<ThreadPool> ResourceManager::GetThreadPool(const std::string &name)
    {
        std::lock_guard<std::mutex> lock(mutex_);

        auto it = thread_pools_.find(name);
        if (it != thread_pools_.end())
        {
            return it->second;
        }

        // 如果不存在,创建默认的
        return CreateThreadPool(name);
    }

    std::shared_ptr<ThreadPool> ResourceManager::CreateThreadPool(
        const std::string &name,
        size_t num_threads)
    {
        std::lock_guard<std::mutex> lock(mutex_);

        if (shutdown_)
        {
            return nullptr;
        }

        // 如果已经存在,返回现有的
        auto it = thread_pools_.find(name);
        if (it != thread_pools_.end())
        {
            std::cout << "[ResourceManager] ThreadPool '" << name << "' already exists" << std::endl;
            return it->second;
        }

        // 创建新的线程池
        auto pool = std::make_shared<ThreadPool>(num_threads, name);
        thread_pools_[name] = pool;

        std::cout << "[ResourceManager] Created ThreadPool '" << name << "'" << std::endl;
        return pool;
    }

    void ResourceManager::RemoveThreadPool(const std::string &name)
    {
        std::lock_guard<std::mutex> lock(mutex_);

        auto it = thread_pools_.find(name);
        if (it != thread_pools_.end())
        {
            // 先关闭线程池
            it->second->Shutdown(true); // 等待任务完成

            // 然后移除
            thread_pools_.erase(it);

            std::cout << "[ResourceManager] Removed ThreadPool '" << name << "'" << std::endl;
        }
    }

    ResourceManager::Statistics ResourceManager::GetStatistics() const
    {
        std::lock_guard<std::mutex> lock(mutex_);

        Statistics stats;
        stats.thread_pool_count = thread_pools_.size();
        stats.object_pool_count = object_pools_.size();
        stats.total_threads = 0;
        stats.total_objects = 0;

        // 统计线程数
        for (const auto &pair : thread_pools_)
        {
            stats.total_threads += pair.second->GetThreadCount();
        }

        return stats;
    }

    void ResourceManager::PrintStatistics() const
    {
        auto stats = GetStatistics();

        std::cout << "\n========== ResourceManager Statistics ==========" << std::endl;
        std::cout << std::setw(20) << "Thread Pools: " << stats.thread_pool_count << std::endl;
        std::cout << std::setw(20) << "Object Pools: " << stats.object_pool_count << std::endl;
        std::cout << std::setw(20) << "Total Threads: " << stats.total_threads << std::endl;
        std::cout << std::setw(20) << "Total Objects: " << stats.total_objects << std::endl;

        // 详细线程池信息
        std::cout << "\nThread Pools Detail:" << std::endl;
        std::lock_guard<std::mutex> lock(mutex_);
        for (const auto &pair : thread_pools_)
        {
            auto pool_stats = pair.second->GetStatistics();
            std::cout << "  " << std::setw(15) << pair.first
                      << ": threads=" << pair.second->GetThreadCount()
                      << ", tasks=" << pool_stats.total_tasks_executed
                      << ", queue=" << pool_stats.max_queue_size << std::endl;
        }

        std::cout << "===============================================" << std::endl;
    }

    void ResourceManager::Shutdown()
    {
        std::lock_guard<std::mutex> lock(mutex_);

        if (shutdown_)
        {
            return;
        }

        std::cout << "[ResourceManager] Shutting down..." << std::endl;

        // 关闭所有线程池
        for (auto &pair : thread_pools_)
        {
            std::cout << "[ResourceManager] Shutting down ThreadPool '" << pair.first << "'" << std::endl;
            pair.second->Shutdown(true); // 等待任务完成
        }

        // 清理对象池
        object_pools_.clear();
        thread_pools_.clear();

        shutdown_ = true;
        std::cout << "[ResourceManager] Shutdown complete" << std::endl;
    }

    void ResourceManager::Clear()
    {
        std::lock_guard<std::mutex> lock(mutex_);

        // 清空所有线程池的任务队列
        for (auto &pair : thread_pools_)
        {
            pair.second->Clear();
        }

        std::cout << "[ResourceManager] Cleared all task queues" << std::endl;
    }

} // namespace common_component
